import { _decorator, Component, EditBox, game, Label, Node, Sprite, SpriteFrame } from 'cc';
import { PropupBase } from '../propup/PropupBase';
import { GlobalVar } from '../GlobalVar';
import { mapNameConfig, ModelGame } from './ModelGame';
import { ModelUI } from '../ModelUI';
import { GameEvent } from '../EventName';
import { GamePanel } from './GamePanel';
const { ccclass, property } = _decorator;

@ccclass('LandSelectPage')
export class LandSelectPage extends PropupBase {

    @property([SpriteFrame])

    imgs: SpriteFrame[] = [];


    @property(Sprite)
    areaShow: Sprite = null;

    @property(EditBox)
    coinEditBox: EditBox = null;
    @property(Node)
    btnDown: Node = null;
    @property(Label)
    nameLabel: Label = null;


    protected onEnable(): void {
        this.show();
    }
    /**每次打开页面执行一次 */
    show() {
        this.coinEditBox.string = "10000";
        ModelGame.getInstance().nowInputCoin = 10000;
        // let coin = Number(this.coinEditBox.string);
        // coin = coin < 10000 ? 10000 : coin;
        // let nowCoin = Number(GlobalVar.userInfo.walletList[0].normalBalance);
        // if (coin > nowCoin) {
        //     this.coinEditBox.string = "10000";
        //     ModelGame.getInstance().nowInputCoin = 10000;
        // } else {
        //     this.coinEditBox.string = String(coin);
        //     ModelGame.getInstance().nowInputCoin = coin;
        // }
    }


    onLeftBtnClick() {
        GlobalVar.areaIndex--;
        if (GlobalVar.areaIndex < 0) {
            GlobalVar.areaIndex = 7;
        }

        this.updateAreaImg();
    }

    onRightBtnClick() {
        GlobalVar.areaIndex++;
        if (GlobalVar.areaIndex > 7) {
            GlobalVar.areaIndex = 0;
        }

        this.updateAreaImg();
    }

    editChange() {
        console.log(Number(this.coinEditBox.string))
        if (Number(this.coinEditBox.string) < 10000) {
            this.coinEditBox.string = "10000"
        }
        ModelGame.getInstance().nowInputCoin = Number(this.coinEditBox.string)
    }


    onAddCoinBtnClick() {
        let coin = Number(this.coinEditBox.string);
        let nowCoin = Number(GlobalVar.userInfo.walletList[0].normalBalance);
        if (coin + 10000 > nowCoin) {
            return;
        }
        let addCoin = coin + 10000;
        if (addCoin <= 0) {
            addCoin = 0;
        }
        ModelGame.getInstance().nowInputCoin = addCoin;
        this.coinEditBox.string = addCoin.toString();
    }

    onMinusCoinBtnClick() {
        let coin = Number(this.coinEditBox.string);

        let addCoin = coin - 10000;
        if (addCoin <= 10000) {
            addCoin = 10000;
        }
        ModelGame.getInstance().nowInputCoin = addCoin;
        this.coinEditBox.string = addCoin.toString();
    }

    updateAreaImg() {
        this.areaShow.spriteFrame = this.imgs[GlobalVar.areaIndex];
        this.nameLabel.string = mapNameConfig[GlobalVar.areaIndex];
    }

    onConfirmBtnClick() {
        if (ModelGame.getInstance().nowInputCoin > Number(GlobalVar.userInfo.walletList[0].normalBalance)) {
            console.log("金币不够")
            game.emit(GameEvent.SHOW_TOAST, "Not enough coin")
            return;
        }
        this.btnDown.active = false;
        ModelUI.getInstance().gamePanel.active = true;
        ModelUI.getInstance().gamePanel.getComponent(GamePanel).initPanel();
    }


    start() {
        this.areaShow.spriteFrame = this.imgs[GlobalVar.areaIndex];
        this.nameLabel.string = mapNameConfig[GlobalVar.areaIndex];
        // this.node.active = false;
    }
    update(deltaTime: number) {

    }
}


